<?php
/**
 * Created by PhpStorm.
 * User: ysongyang
 * Date: 2016/6/8
 * Time: 9:46
 */
namespace Admin\Controller;

class NavsController extends CommonController{

    function _initialize()
    {
        parent::_initialize();
        $this->_navs = M('navs');
    }


    public function index()
    {
        $this->navs = $this->_navs ->order('nav_order asc')->select();
        $this->display();
    }


    public function add(){
        if(IS_POST){
            $res = $this->_navs ->add($_POST);
            if($res){
                $this->success('添加自定义导航成功！', U(MODULE_NAME . '/Navs/index'));
            }else{
                $this->error('添加自定义导航失败！');
            }
        }else{
            $this->display();
        }
    }


    public function edit(){
        if(IS_POST){
            $nav_id = $_GET['nav_id'];
            $res = $this->_navs ->where(array('nav_id'=>$nav_id))->save($_POST);
            if($res){
                $this->success('修改自定义导航成功！', U(MODULE_NAME . '/Navs/index'));
            }else{
                $this->error('修改自定义导航失败！');
            }
        }else{
            $nav_id = I('nav_id');
            $this->nav = $this->_navs ->where(array('nav_id'=>$nav_id))->find();
            $this->display();
        }
    }


    //异步更新排序
    public function change(){
        $nav_id = $_POST['nav_id'];
        $nav_order = $_POST['nav_order'];
        $res = $this->_navs->where(array('nav_id'=>$nav_id))->save(array('nav_order'=>$nav_order));
        if($res){
            $data = ['status'=>0,'info'=>'排序更新成功！'];
        }else{
            $data = ['status'=>1,'info'=>'排序更新失败，请稍后再试！',];
        }
        $this->ajaxReturn($data);
    }


    /**
     * 异步删除操作
     */
    public function delete(){
        $res=$this->_navs->where(array('nav_id'=>$_POST['nav_id']))->delete();
        if($res){
            $data =array('status'=>1,'info'=>'删除自定义导航成功！');
        }else{
            $data = array('status'=>0,'info'=>'删除自定义导航失败，请稍后再试！');
        }
        $this->ajaxReturn($data);
    }
}